<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head lang="en" th:replace="include/common/common :: commonHeader">
    <footer th:include="include/common/common :: layUiResource"></footer>
    <!--    <footer th:include="include/common/common :: bootstrapResource"></footer>-->
    <footer th:include="include/common/common :: blueCSSResource"></footer>
    <footer th:include="include/common/common :: zTreeCSSSource"></footer>
    <footer th:include="include/common/common :: zTreeJSSource"></footer>

    <script>
        layui.config({
            base: '/staticResource/layuiadmin/' //静态资源所在路径
        }).extend({
            index: 'lib/index' //主入口模块
        }).use(['index' , 'laytpl'] , function () {
            var layer = layui.layer
                ,element = layui.element
                ,admin = layui.admin
                ,$ = layui.jquery;

            element.render();

            let isUpdateTree = [[${isUpdateTree}]]
                ,data = JSON.parse($("#zTreeOption" , window.parent.document).val())
                ,_isExpandAll = $.fn.blueJs.tools.isNotEmpty(data.isExpandAll)?data.isExpandAll:false
                ,userTreeOptions = {
                    treeId:'zTree',
                    title:data.title,
                    url:data.url,
                    echoId:data.echoId,  //回显元素id
                    // clickSubmit:'id',    //默认点击提交值 属性名称
                    submitAttributeName:data.submitAttributeName,  //提交属性名称
                    excludeCode:data.excludeCode, //排除节点ID
                    isShowCode:data.isShowCode,   //是否显示节点代码 默认不显示
                    isExpandAll:_isExpandAll, //
                    canSelectRoot:data.canSelectRoot,     //根节点 是否允许选择，默认可选
                    canSelectParent:data.canSelectParent,    //父节点 是否允许选择，默认可选
                    setting : {
                        check: {
                            enable: true//checkbox
                        },
                        // 回调定义
                        callback:{
                            beforeClick: zTreeBeforeClick ,
                            // 鼠标点击事件的回调函数
                            onClick:zTreeOnClick
                        }
                    }
                };

            //加载tree
            function loadTree(){
                $.fn.blueJs.zTreeTool.loadTree(userTreeOptions);
            };

            // 单击前的回调函数
            function zTreeBeforeClick(treeId, treeNode, clickFlag) {
                let flag = treeNode.isTreeLeaf || userTreeOptions.canSelectParent;
                flag = (!userTreeOptions.canSelectRoot && treeNode.isRoot)?false:flag;

                // 非末级节点时，如果不允许选择，则展开/收起节点
                if (!flag){
                    layer.alert("当前节点不允许选择！");
                    /*let treeObj = $.fn.blueJs.zTreeTool.getTreeObj(userTreeOptions.treeId);
                    treeObj.expandNode(treeNode);*/

                    // alert("非末级节点，不允许选择！");
                }
                return flag;
            }

            /**
             * treeNode鼠标单击 回调
             * @param event
             * @param treeId
             * @param treeNode
             */
            function zTreeOnClick(event, treeId, treeNode) {
                $.fn.blueJs.tools.isNotEmpty(userTreeOptions.echoId)?window.parent.document.getElementById(userTreeOptions.echoId + "Code").value = treeNode.id:null;
                if ($.fn.blueJs.tools.isNotEmpty(userTreeOptions.echoId)){
                    let submitAttributeName = userTreeOptions.submitAttributeName;
                    if (submitAttributeName != 'id' && $.fn.blueJs.tools.isNotEmpty(submitAttributeName)){
                        $("#" + userTreeOptions.echoId + "Code" , window.parent.document).val(treeNode[submitAttributeName]);
                    }else {
                        $("#" + userTreeOptions.echoId + "Code" , window.parent.document).val(treeNode.id);
                    }
                    $("#" + userTreeOptions.echoId + "Name" , window.parent.document).val(treeNode.name);
                }
                if (isUpdateTree){
                    let newTreeNode = $.fn.blueJs.zTreeTool.getNewTreeAttribute(userTreeOptions , treeNode.id , true);
                    $.fn.blueJs.tools.isNotEmpty($("#treeSort" , window.parent.document))?$("#treeSort" , window.parent.document).val(newTreeNode.treeSort):null;
                    $.fn.blueJs.tools.isNotEmpty($("#treeCode" , window.parent.document))?$("#treeCode" , window.parent.document).val(newTreeNode.treeCode):null;
                }
                let objectType = data.objectType , objectUrl = data.objectUrl , objectEl = data.objectEl;

                if ($.fn.blueJs.tools.isNotEmpty(objectType)){
                    let jsonData = {
                        type : objectType,
                        entityId : treeNode.id
                    }

                    $.ajax({
                        type:'POST',
                        url:objectUrl,
                        dataType:'JSON',
                        cache:false,
                        data:jsonData,
                        success:function (result) {
                            $("#" + objectEl + "Code" , window.parent.document).val(result.id);
                            $("#" + objectEl + "Name" , window.parent.document).val(result.name);
                        },
                        error:function () {

                        }
                    });

                }

                let index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                parent.layer.close(index);
            };

            $(document).ready(function(){
                loadTree();
            });

            //点击事件监听
            $(document).on('click' , function (event) {
                let targetId = event.target.id;
                let _searchBtnId = 'searchBtn' , _searchInputId = 'searchInput';
                if (targetId == _searchBtnId || targetId == _searchInputId){
                    fuzzSearch('zTree','#' + _searchInputId,null,true); //初始化模糊搜索方法
                }
            });
        });
    </script>
</head>
<body>
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body">
                    <div class="form-group treeSearchInput" id="search" style="display: block">
                        <!--                <label for="searchInput">关键字：</label>-->
                        <input type="text" id="searchInput" maxlength="50" class="empty"/>
                        <button class="btn" id="searchBtn" >搜索</button>
                    </div>

                    <div id="zTree" class="ztree"></div>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
</html>